package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"repo-man20/cache"
	"repo-man20/models"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_code := c.PostForm("car_code")
	car_name := c.PostForm("car_name")
	car_num := c.PostForm("car_num")
	if car_code == "" || car_name == "" || car_num == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	add := models.Car{
		CarCode: car_code,
		CarName: car_name,
		CarNum:  car_num,
	}
	models.DB.Create(&add).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_address := c.PostForm("order_address")
	no_address := c.PostForm("no_address")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if order_code == "" || order_address == "" || no_address == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	add := models.Order{
		OrderCode:    order_code,
		OrderAddress: order_address,
		NoAddress:    no_address,
		Cid:          cid,
	}
	models.DB.Create(&add).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_name as CarName,
		  cars.car_num as CarNum,
		  orders.order_code as OrderCode,
		  orders.order_address as OrderAddress,
		  orders.no_address as NoAddress
		FROM
		  orders
		  LEFT JOIN cars ON orders.cid = cars.id
	*/
	var list []models.CarOrderList
	redis_key := "car_data_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_num as CarNum",
				"orders.order_code as OrderCode",
				"orders.order_address as OrderAddress",
				"orders.no_address as NoAddress",
			).
			Joins("LEFT JOIN cars ON orders.cid = cars.id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var count int64
	models.DB.Model(&models.CarOrderList{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"count": count,
		"list":  list,
	})
}
func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_name as CarName,
		  cars.car_num as CarNum,
		  orders.order_code as OrderCode,
		  orders.order_address as OrderAddress,
		  orders.no_address as NoAddress
		FROM
		  orders
		  LEFT JOIN cars ON orders.cid = cars.id
	*/
	var list []models.CarOrderList
	redis_key := "car_data_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_num as CarNum",
				"orders.order_code as OrderCode",
				"orders.order_address as OrderAddress",
				"orders.no_address as NoAddress",
			).
			Joins("LEFT JOIN cars ON orders.cid = cars.id").
			Where("orders.id = ?", id).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var count int64
	models.DB.Model(&models.CarOrderList{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"list": list,
	})
}
